package com.zhang.webadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.webadmin.mapper.AclUserMapper;
import com.zhang.webadmin.pojo.*;
import com.zhang.webadmin.service.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author jf
 */
@Service
public class AclUserServiceImpl extends ServiceImpl<AclUserMapper, AclUser>
        implements AclUserService {

    @Resource
    private AclUserRoleService aclUserRoleService;
    @Resource
    private AclRoleService aclRoleService;
    @Resource
    private AclRolePermissionService aclRolePermissionService;
    @Resource
    private AclPermissionService aclPermissionService;

    @Override
    public List<Long> hasRole(Long aclUserId) {
        List<AclUserRole> userRoleList = aclUserRoleService.lambdaQuery()
                .eq(AclUserRole::getUserId, aclUserId)
                .list();
        if (userRoleList.isEmpty()) {
            return List.of();
        }
        List<AclRole> roleList = aclRoleService.lambdaQuery()
                .in(AclRole::getId, userRoleList.stream().map(AclUserRole::getRoleId).toList())
                .list();

        return roleList.stream().map(AclRole::getId).toList();
    }

    @Override
    public void setRole(long userId, List<Long> roleIdList) {
        // 删除之前存在的记录
        LambdaQueryWrapper<AclUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AclUserRole::getUserId, userId);
        aclUserRoleService.remove(wrapper);

        if (roleIdList == null || roleIdList.isEmpty()) {
            return;
        }
        // 查询哪些role存在
        List<AclRole> roleList = aclRoleService.lambdaQuery().in(AclRole::getId, roleIdList).list();
        Set<Long> roleIdSet = roleList.stream().map(AclRole::getId).collect(Collectors.toSet());

        // 保存新的
        for (Long roleId : roleIdSet) {
            AclUserRole add = new AclUserRole();
            add.setUserId(userId);
            add.setRoleId(roleId);
            add.setCreateTime(LocalDateTime.now());
            add.setUpdateTime(LocalDateTime.now());

            aclUserRoleService.save(add);
        }
    }

    @Override
    public List<AclPermission> queryPermission(long aclUserId) {
        List<AclUserRole> userRoleList = aclUserRoleService.lambdaQuery().eq(AclUserRole::getUserId, aclUserId).list();
        if (userRoleList == null || userRoleList.isEmpty()) {
            return List.of();
        }
        List<AclRole> roleList = aclRoleService.lambdaQuery()
                .in(AclRole::getId, userRoleList.stream().map(AclUserRole::getRoleId).toList())
                .list();
        if (roleList == null || roleList.isEmpty()) {
            return List.of();
        }
        List<AclRolePermission> rolePermissionList = aclRolePermissionService.lambdaQuery()
                .in(AclRolePermission::getRoleId, roleList.stream().map(AclRole::getId).toList())
                .list();
        if (rolePermissionList == null || rolePermissionList.isEmpty()) {
            return List.of();
        }
        return aclPermissionService.lambdaQuery()
                .in(AclPermission::getId, rolePermissionList.stream().map(AclRolePermission::getPermissionId).toList())
                .list();
    }
}




